package mapreduce.wordcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 *继承Reducer类，在其reduce方法中编写业务逻辑。将k2，v2转为k3，v3
 * 有四个泛型类型:<KEYIN,VALUEIN,KEYOUT,VALUEOUT>，就是k2,v2,k3,v3.
 */
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    /**
     *
     * @param key    k2
     * @param values    从map阶段过来的k2实际上还经过一个shuffle阶段。v2实际上是一个集合
     * @param context
     */
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0;    //统计每个词的频度
        for (LongWritable value : values){
            count += value.get();
        }

        context.write(key, new LongWritable(count));
    }
}
